#include "OCMain/FileUtil.h"
#include "OCMain/Environment.h"
#include "OCMain/Log.h"
#include "OCServer/ServerUtil.h"
#include "timer/GFSFetcher.h"

using namespace OCPP;

GFSFetcher* gGFSFetcher = NULL;

void init()
{
	String pluginName = "OCPP_ControllerWeatherService";

	String yamlFileName = joinPath(Singleton(Environment).getAppPath(), "config", pluginName + ".yml");

	try {
		YAML::Node node = YAML::LoadFile(yamlFileName);
		YAML::Node timer = node["timer"];
		YAML::Node gfs = timer["gfs"];
		bool enable = getYmlValue<bool>(gfs, "enable", false);
		if (enable)
		{
			String cachePath = getYmlValue<String>(gfs, "cachePath", "");
			if (cachePath.empty())return;
			int numHourData = getYmlValue<int>(gfs, "numHourData", 24);
			gGFSFetcher = new GFSFetcher();
			gGFSFetcher->setName(pluginName);
			gGFSFetcher->setFetchSeconds(3600);
			gGFSFetcher->setClearSeconds(3600);
			gGFSFetcher->setDataPath(cachePath);
			gGFSFetcher->setCachePath(cachePath);
			gGFSFetcher->setNumHourData(numHourData);
			gGFSFetcher->startTimer();
		}
	}
	catch (const YAML::Exception& e) {
		LOG_ERROR << e.msg << "\n";
	}
}

void release()
{
	CORE_SAFE_DELETE(gGFSFetcher);
}